package com.qh.book.converter;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qh.book.dataobj.OrderDetail;
import com.qh.book.dto.CartDTO;
import com.qh.book.dto.OrderDTO;
import com.qh.book.enums.ResultEnum;
import com.qh.book.exception.BsException;
import com.qh.book.form.OrderForm;

/**
 * 
 * @author qihong
 * @date 2017年10月5日 下午8:15:09 
 * @Description: TODO 看类名
 */

public class OrderForm2OrderDTO {

	private final static Logger log = LoggerFactory.getLogger(OrderForm2OrderDTO.class);

    public static OrderDTO convert(OrderForm orderForm) {
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();

        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());

        List<OrderDetail> orderDetailList = new ArrayList<OrderDetail>();
        try {
        	orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
        } catch (Exception e) {
        	log.error("【对象转换】错误, string={}", orderForm.getItems());
            throw new BsException(ResultEnum.PARM_FAIL);
        }
        orderDTO.setOrderDetailList(orderDetailList);

        return orderDTO;
    }
}
